//
//  ExchangeCell.m
//  YiBaoStore
//
//  Created by 毛云河 on 2019/1/5.
//  Copyright © 2019 小河. All rights reserved.
//

#import "ExchangeCell.h"

@implementation ExchangeCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self createView];
    }
    
    return self;
}

- (void)createView
{
    self.backgroundColor = [UIColor whiteColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 1)];
    [self.contentView addSubview:lineView];
    lineView.backgroundColor = RGBACOLOR(241, 241, 241, 1);
    
    self.mainIconImage = [[UIImageView alloc] init];
    [self.contentView addSubview:self.mainIconImage];
    self.mainIconImage.backgroundColor = [UIColor lightGrayColor];
    self.mainIconImage.contentMode = UIViewContentModeScaleAspectFill;
    self.mainIconImage.clipsToBounds = YES;
    
    self.ziCaiLabel = [[UILabel alloc] init];
    [self.mainIconImage addSubview:self.ziCaiLabel];
    self.ziCaiLabel.backgroundColor = RGBACOLOR(239, 208, 83, 0.9);
    self.ziCaiLabel.font = FontWithSize(12);
    self.ziCaiLabel.textColor = [UIColor whiteColor];
    self.ziCaiLabel.textAlignment = 1;
    self.ziCaiLabel.text = @"自采";
    
    self.titleLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.titleLabel];
    self.titleLabel.textAlignment = 0;
    self.titleLabel.textColor = BlackTextColor;
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.font = FontWithSize(14);
    
    self.stickBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.stickBtn];
    [self.stickBtn setBackgroundImage:[UIImage imageNamed:@"stickBtn_icon"] forState:UIControlStateNormal];
    
    self.codeLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.codeLabel];
    self.codeLabel.textAlignment = 0;
    self.codeLabel.textColor = RGBACOLOR(194, 194, 194, 1);
    self.codeLabel.font = FontWithSize(12);
    
    self.priceLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.priceLabel];
    self.priceLabel.textAlignment = 0;
    self.priceLabel.textColor = BlackTextColor;
    self.priceLabel.font = FontWithSize(14);
    
    self.deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.deleteBtn];
    self.deleteBtn.titleLabel.font = FontWithSize(12);
    self.deleteBtn.backgroundColor = BtnBgColor;
    [self.deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
    [self.deleteBtn setTitleColor:RGBACOLOR(2, 2, 0, 1) forState:UIControlStateNormal];
    self.deleteBtn.layer.cornerRadius = 15;
    self.deleteBtn.layer.masksToBounds = YES;
    
    self.editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.editBtn];
    self.editBtn.titleLabel.font = FontWithSize(12);
    self.editBtn.backgroundColor = [UIColor whiteColor];
    [self.editBtn setTitle:@"编辑" forState:UIControlStateNormal];
    [self.editBtn setTitleColor:RGBACOLOR(2, 2, 0, 1) forState:UIControlStateNormal];
    self.editBtn.layer.cornerRadius = 15;
    self.editBtn.layer.masksToBounds = YES;
    self.editBtn.layer.borderWidth = 1;
    self.editBtn.layer.borderColor = BtnBgColor.CGColor;
    
//    self.allNumberLabel = [[UILabel alloc] init];
//    [self.contentView addSubview:self.allNumberLabel];
//    self.allNumberLabel.textAlignment = 0;
//    self.allNumberLabel.textColor = [UIColor lightGrayColor];
//    self.allNumberLabel.font = FontWithSize(12);
    
    self.surplusNumberLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.surplusNumberLabel];
    self.surplusNumberLabel.textAlignment = 0;
    self.surplusNumberLabel.textColor = [UIColor redColor];
    self.surplusNumberLabel.font = FontWithSize(12);
}

- (void)layoutSubviews
{
    self.mainIconImage.frame = CGRectMake(15, 15, 100, 100);
    self.titleLabel.frame = CGRectMake(self.mainIconImage.right+10, 15, SCREENWIDTH-self.mainIconImage.right-10-15-20-10, 44);
    self.stickBtn.frame = CGRectMake(SCREENWIDTH-15-20, 20, 20, 20);
    self.ziCaiLabel.frame = CGRectMake(0, self.mainIconImage.height-30, self.mainIconImage.width, 30);
    self.codeLabel.frame = CGRectMake(self.mainIconImage.right+10, self.titleLabel.bottom+5, 100, 12);
    [self.codeLabel sizeToFit];
    self.priceLabel.frame = CGRectMake(self.mainIconImage.right+10, self.codeLabel.bottom+18, 120, 14);
    [self.priceLabel sizeToFit];
    self.deleteBtn.frame = CGRectMake(SCREENWIDTH-70-15, self.titleLabel.bottom+26, 70, 30);
    self.editBtn.frame = CGRectMake(self.deleteBtn.left-10-70, self.titleLabel.bottom+26, 70, 30);
    
//    self.allNumberLabel.frame = CGRectMake(self.priceLabel.left, self.priceLabel.bottom+11, 30, 12);
//    [self.allNumberLabel sizeToFit];
    
    self.surplusNumberLabel.frame = CGRectMake(self.priceLabel.left, self.priceLabel.bottom+11, 200, 12);
    [self.surplusNumberLabel sizeToFit];
}

- (void)setCellWithMode:(MainModel *)model
{
    [self.mainIconImage sd_setImageWithURL:[NSURL URLWithString:model.showImage] placeholderImage:[UIImage imageNamed:@"zhanwei100"]];
    self.titleLabel.text = [NSString stringWithFormat:@"%@",model.title];
    self.codeLabel.text = [NSString stringWithFormat:@"条码 %@",model.barcode];
    float promotionPrice = [model.promotionPrice floatValue];
    self.priceLabel.text = [NSString stringWithFormat:@"￥%0.2f",promotionPrice];
    self.surplusNumberLabel.text = [NSString stringWithFormat:@"剩余%@件",model.stockAmount];
}

@end
